package com.haixue.android.academy.view.tiku;
import android.academy.haixue.com.haixue.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.util.Log;

import java.io.File;

public class ImageGetterFromSDCard extends BaseImageGetter {
	
	private String tag="ImageGetterFromSDCard";

	public ImageGetterFromSDCard(Context context) {
		// TODO Auto-generated constructor stub
		super(context);
	}
	
	@SuppressWarnings("deprecation")
	@Override
	public Drawable getDrawable(String source) {
		// TODO Auto-generated method stub
		
		if (source.indexOf("file://")>=0) {
			
			if (Environment.getExternalStorageState().equals(
					Environment.MEDIA_MOUNTED)) {
				
				File f = new File(source.replace("file://", ""));
				
				if (f.exists()) {
					
					BitmapDrawable d = null;
					
					Bitmap bmp =BitmapFactory.decodeFile(source.replace("file://", ""));
					
					DisplayMetrics dm = context.getResources().getDisplayMetrics();
					
					float density  = dm.density;      // 屏幕密度（像素比例：0.75/1.0/1.5/2.0）
					
					bmp.setDensity(dm.densityDpi);
					
					int height = bmp.getHeight();
					
					Log.d(tag, "height = "+height);
					
					d = new BitmapDrawable(bmp);
					
					if (width!=-1) {
					
						return DrawableZoomer.zoomDrawableByWidth(d, width);
						
					}
					
					int imageDisplayWidth = (int) (bmp.getWidth()*density);
					
					if (maxWidth>0&&imageDisplayWidth>maxWidth) {
						imageDisplayWidth = maxWidth;
					}
					
					
					Log.d(tag, source+" width is "+bmp.getWidth());

					Log.d(tag, "density = "+density);
					
					Log.d(tag, "1dp = "+context.getResources().getDimensionPixelSize(R.dimen.image_size_1dp));
					
					Log.d(tag, "image display width is "+imageDisplayWidth);
					
					return DrawableZoomer.zoomDrawableByWidth(d,imageDisplayWidth);
				
				}

			} 
			
		}
		
		return null;
	}
	
	
}
